import NProgress from 'nprogress';
import 'nprogress/nprogress.css';
import router from './router';
import useStore from '@/store';

NProgress.configure({ showSpinner: false });

let dataFlag = false; // 标识：是否为第一次进入系统

router.beforeEach(async (to: any, from: any, next) => {

  NProgress.start();

  if (!dataFlag) {
    dataFlag = true;
    const { user } = useStore();
    await user.getTopMenuList()
    if (localStorage.getItem('token')) user.getUserProject()
    const routes = [] as any
    router.options.routes.forEach(item => {
      if (item.path === '/') {
        routes.push(...item.children)
      } else if (item.name) {
        routes.push(item)
      }
    })
    user.setMenu(routes || []);
    NProgress.done();
    next({ ...to, replace: true });
  } else {
    NProgress.done();
    next();
  }
});
